<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blogs/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blogs/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blogs/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blogs/images/apple-touch-icon.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/blogs/images/favicon-32x32.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/blogs/images/favicon-16x16.png?v=5.1.3">


  <link rel="mask-icon" href="/blogs/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="JavaScript," />










<meta name="description" content="本文从JS对宏队列和微队列中的任务出发，详解JS的事件循环机制 1.事件是怎么执行的？主线程（JavaScript引擎的线程）在运行过程中会产生一个东西叫做执行栈。而执行栈中，存在两个东西，一个叫堆，一个叫栈。堆里面存放着一些非结构化的数据或者是对象的内存空间。栈里面则是当前需要执行的代码。这些执行代码调用各种 api (如：dom事件，ajax, setTimeout，Promise等)。并往任">
<meta property="og:type" content="article">
<meta property="og:title" content="JS事件循环机制">
<meta property="og:url" content="https://louchaoyy.gitee.io/blogs/2022/07/10/JS%E4%BA%8B%E4%BB%B6%E5%BE%AA%E7%8E%AF%E6%9C%BA%E5%88%B6/index.html">
<meta property="og:site_name" content="Chao&#39;s blog">
<meta property="og:description" content="本文从JS对宏队列和微队列中的任务出发，详解JS的事件循环机制 1.事件是怎么执行的？主线程（JavaScript引擎的线程）在运行过程中会产生一个东西叫做执行栈。而执行栈中，存在两个东西，一个叫堆，一个叫栈。堆里面存放着一些非结构化的数据或者是对象的内存空间。栈里面则是当前需要执行的代码。这些执行代码调用各种 api (如：dom事件，ajax, setTimeout，Promise等)。并往任">
<meta property="og:locale">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/07/10/JS%E4%BA%8B%E4%BB%B6%E5%BE%AA%E7%8E%AF%E6%9C%BA%E5%88%B6/2.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/07/10/JS%E4%BA%8B%E4%BB%B6%E5%BE%AA%E7%8E%AF%E6%9C%BA%E5%88%B6/3.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/07/10/JS%E4%BA%8B%E4%BB%B6%E5%BE%AA%E7%8E%AF%E6%9C%BA%E5%88%B6/1.png">
<meta property="article:published_time" content="2022-07-10T02:30:54.000Z">
<meta property="article:modified_time" content="2022-07-10T14:15:21.273Z">
<meta property="article:author" content="Chaoyxy">
<meta property="article:tag" content="JavaScript">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://louchaoyy.gitee.io/blogs/2022/07/10/JS%E4%BA%8B%E4%BB%B6%E5%BE%AA%E7%8E%AF%E6%9C%BA%E5%88%B6/2.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '',
    scheme: 'Gemini',
    version: '5.1.3',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://louchaoyy.gitee.io/blogs/2022/07/10/JS事件循环机制/"/>





  <title>JS事件循环机制 | Chao's blog</title>
  








<meta name="generator" content="Hexo 5.4.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blogs/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Chao's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blogs/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/blogs/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blogs/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://louchaoyy.gitee.io/blogs/blogs/2022/07/10/JS%E4%BA%8B%E4%BB%B6%E5%BE%AA%E7%8E%AF%E6%9C%BA%E5%88%B6/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blogs/images/cat.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Chao's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">JS事件循环机制</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2022-07-10T10:30:54+08:00">
                2022-07-10
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/blogs/2022/07/10/JS%E4%BA%8B%E4%BB%B6%E5%BE%AA%E7%8E%AF%E6%9C%BA%E5%88%B6/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2022/07/10/JS事件循环机制/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>本文从JS对宏队列和微队列中的任务出发，详解JS的事件循环机制</p>
<h2 id="1-事件是怎么执行的？"><a href="#1-事件是怎么执行的？" class="headerlink" title="1.事件是怎么执行的？"></a>1.事件是怎么执行的？</h2><p>主线程（JavaScript引擎的线程）在运行过程中会产生一个东西叫做执行栈。而执行栈中，存在两个东西，一个叫堆，一个叫栈。堆里面存放着一些非结构化的数据或者是对象的<a target="_blank" rel="noopener" href="https://so.csdn.net/so/search?q=%E5%86%85%E5%AD%98&spm=1001.2101.3001.7020">内存</a>空间。栈里面则是当前需要执行的代码。这些执行代码调用各种 <code>api</code> (如：dom事件，<a target="_blank" rel="noopener" href="https://so.csdn.net/so/search?q=ajax&spm=1001.2101.3001.7020">ajax</a>, setTimeout，Promise等)。并往任务队列中添加各种事件，待执行栈中代码执行完毕后，会依次取出任务队列中的数据放入到执行栈中继续执行。</p>
<ul>
<li>主线程运行时，会产生执行栈（执行栈可以理解为代码运行的容器）,栈内代码执行过程中当遇到任务源时，将任务源中的回调函数加入到任务队列中</li>
<li>执行栈中的代码执行完毕后，会依次调用任务队列中的代码放入到执行栈中执行，如果反复循环。就形成了我们所说的事件循环 <code>Event loop</code></li>
</ul>
<p>针对于不同的任务，JS会把不同的任务放进相映的队列，然后依次执行</p>
<p>主要分为三大类</p>
<ul>
<li>定时器类</li>
<li>Promise类</li>
<li>同步任务类</li>
</ul>
<span id="more"></span>

<h2 id="2-同步异步代码执行流程"><a href="#2-同步异步代码执行流程" class="headerlink" title="2.同步异步代码执行流程"></a>2.同步异步代码执行流程</h2><p>1、在同一轮事件循环当中，<strong>同步任务会直接放入到主线程的执行栈进行执行，而异步任务不进入执行栈，而是进入任务<a target="_blank" rel="noopener" href="https://so.csdn.net/so/search?q=%E9%98%9F%E5%88%97&spm=1001.2101.3001.7020">队列</a>中</strong></p>
<p>2、同步任务按顺序执行，执行完毕后，才会去读取队列中的可执行的异步任务，并将可执行的异步任务依次加入到执行栈中执行。如此反复循环</p>
<img src="/blogs/2022/07/10/JS%E4%BA%8B%E4%BB%B6%E5%BE%AA%E7%8E%AF%E6%9C%BA%E5%88%B6/2.png" class title="同步异步代码执行的流程">

<h2 id="3-宏任务和微任务"><a href="#3-宏任务和微任务" class="headerlink" title="3.宏任务和微任务"></a>3.宏任务和微任务</h2><h3 id="1-宏任务（macrotask）"><a href="#1-宏任务（macrotask）" class="headerlink" title="1.宏任务（macrotask）"></a>1.宏任务（macrotask）</h3><p>可以理解为每次执行栈中执行的代码</p>
<ol>
<li>宏任务在执行过程中可以创建宏任务，也可以创建微任务，创建的宏任务会加入到宏任务队列中，等待下一个循环执行。而创建的微任务会在本次宏任务中的代码全部执行完成以后立刻依次执行。故宏任务中创建的微任务不会影响当前宏任务的执行</li>
<li>当一个宏任务执行完以后，会读取微任务队列中的任务，并依次全部执行完微任务队列中的任务。当微任务队列中的任务全部执行完成后，算是一个完整的task执行完成。</li>
<li>浏览器为了能够使得JS内部task与DOM任务能够有序的执行，会在一个task执行结束后，在下一个 task 执行开始前，对页面进行重新渲染 。如此过程不断重复，便是我们所说的事件循环，Event loop</li>
<li>常见的宏任务： <code>setTimeout</code>, <code>setInterval</code>, <code>requestAnimationFrame</code>, <code>I/O</code>, <code>script标签内代码</code>，<code>setImmediate</code> (Node环境中) 等（这些也被称作任务源）</li>
</ol>
<h3 id="2-微任务（microtask）"><a href="#2-微任务（microtask）" class="headerlink" title="2.微任务（microtask）"></a>2.微任务（microtask）</h3><p>可以简单的理解为当前宏任务执行完成后立刻被执行的任务</p>
<ol>
<li>微任务执行期在 本次宏任务执行完之后，下个宏任务执行之前，并且在UI渲染之前</li>
<li>微任务中创建的宏任务，也会被加入宏任务队列中，等待依次执行</li>
<li>微任务中创建的微任务，会在当前task中执行完成，也就是说微任务中创建的微任务，会在下一个宏任务执行前以及UI渲染前被执行</li>
<li>常见的微任务有： <code>Promise callback</code>， <code>process.nextTick</code>（node环境中），<code>MutationObserver</code> （这些也被称作任务源）</li>
</ol>
<h3 id="3-事件循环流程总结"><a href="#3-事件循环流程总结" class="headerlink" title="3.事件循环流程总结"></a>3.事件循环流程总结</h3><p>1、主线程开始执行一段代码, 假设开始执行一个 <code>script</code> 标签内的代码，将代码放入执行栈中执行，同步代码优先执行，执行过程中，当遇到任务源时，判断是宏任务还是微任务<br>2、 如果是宏任务，加入到宏任务队列中，如果是微任务，加入到微任务队列中<br>3、 同步代码执行完成，执行栈空闲，检查微任务队列中是否有可执行任务，如果有，依次执行所有微任务队列中的任务。如果没有。当前任务执行结束。<br>4、 渲染UI。<br>5、 检查宏任务队列是否有可执行的宏任务，如果有，取出队列中最前面的那个宏任务，加入到执行栈中开始执行，然后重复 步骤1- 5。直到宏任务队列中所有任务执行结束。</p>
<img src="/blogs/2022/07/10/JS%E4%BA%8B%E4%BB%B6%E5%BE%AA%E7%8E%AF%E6%9C%BA%E5%88%B6/3.png" class title="同步异步代码执行的流程">

<h2 id="4-同异步任务"><a href="#4-同异步任务" class="headerlink" title="4.同异步任务"></a>4.同异步任务</h2><p><strong>同步任务&gt;Promise&gt;Timer</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;1&#x27;</span>);</span><br><span class="line">&#125;, <span class="number">0</span>);</span><br><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line">    <span class="attr">func</span>: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;a</span><br><span class="line">        <span class="built_in">setTimeout</span>(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">&#x27;2&#x27;</span>)</span><br><span class="line">        &#125;, <span class="number">0</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="keyword">function</span> (<span class="params">resolve</span>) </span>&#123;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">&#x27;3&#x27;</span>);</span><br><span class="line">            resolve();</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line">obj.func().then(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;4&#x27;</span>)</span><br><span class="line">&#125;);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&#x27;5&#x27;</span>);</span><br><span class="line"><span class="comment">// 3, 5, 4, 1, 2</span></span><br></pre></td></tr></table></figure>

<p>执行过程</p>
<p><strong>先把定时器1入宏队列，定义对象，往下执行，obj.fun()时，进入对象函数，把定时器2入宏队列，执行返回的promise对象（同步任务），输出3，进入.then，把.then注册为微任务，入队列。执行最后一句输出5，此时输出为 3,5</strong></p>
<p><strong>检查当前宏任务下的微队列任务，发现4还没有执行，输出4。发现已经没有微任务了，检查下一个宏任务，发现定时器1还没执行，输出1，检查当前微队列，为空。执行下一个宏任务，输出定时器2的2。</strong></p>
<p><strong>至此，输出顺序为 3,5,4,1,2</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="number">1</span>)</span><br><span class="line"><span class="keyword">const</span> p = <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="keyword">function</span> (<span class="params">resolve, reject</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="number">2</span>)</span><br><span class="line">    resolve()</span><br><span class="line">&#125;).then(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="number">3</span>)</span><br><span class="line">    <span class="keyword">throw</span> (<span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">&#x27;错误&#x27;</span>))</span><br><span class="line">&#125;).catch(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="number">4</span>)</span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">console</span>.log(<span class="number">5</span>)</span><br><span class="line"><span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="number">6</span>)</span><br><span class="line">&#125;, <span class="number">0</span>);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="number">7</span>)</span><br><span class="line">p.then(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="number">8</span>)</span><br><span class="line">    <span class="keyword">throw</span> (<span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">&#x27;错误2&#x27;</span>))</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// 1, 2, 5, 7, 3, 4, 8, 报错(没有相邻的catch处理&#x27;错误2&#x27;), 6</span></span><br></pre></td></tr></table></figure>

<p>执行过程：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">输出1，没什么好说的</span><br><span class="line"></span><br><span class="line">声明新的promise，当做同步任务执行，输出2，把resolve放入微队列。</span><br><span class="line"></span><br><span class="line">把.then和.catch分别放入当前宏任务的微队列</span><br><span class="line"></span><br><span class="line">输出5，没什么好说的</span><br><span class="line"></span><br><span class="line">把定时器放入下一个宏任务，入队列</span><br><span class="line"></span><br><span class="line">输出7，没什么可说的</span><br><span class="line"></span><br><span class="line">把p.then放入当前宏任务下的微队列。</span><br><span class="line"></span><br><span class="line">检查当前宏任务下的所有微任务，执行第一个.then，输出3，执行.catch,输出4，</span><br><span class="line"></span><br><span class="line">执行p.then，输出8，执行到throw (new Error(&#x27;错误2&#x27;))的时候，抛出异常需要被catch捕获，由于没有对应的catch处理，因此报错</span><br><span class="line"></span><br><span class="line">执行下一个宏任务（定时器任务），输出6</span><br><span class="line"></span><br><span class="line">至此，程序执行完成，输出顺序为：1,2,5,7,3,4,8，程序报错，6</span><br></pre></td></tr></table></figure>

<p><strong>当我们对该程序改造一下，假如p.then()里面对应的异常被捕获了，那么执行结果就应该是这样的</strong></p>
<p>改造代码p.then()</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="number">1</span>)</span><br><span class="line"><span class="keyword">const</span> p = <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="keyword">function</span> (<span class="params">resolve, reject</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="number">2</span>)</span><br><span class="line">    resolve()</span><br><span class="line">&#125;).then(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="number">3</span>)</span><br><span class="line">    <span class="keyword">throw</span> (<span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">&#x27;错误&#x27;</span>))</span><br><span class="line">&#125;).catch(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="number">4</span>)</span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">console</span>.log(<span class="number">5</span>)</span><br><span class="line"><span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="number">6</span>)</span><br><span class="line">&#125;, <span class="number">0</span>);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="number">7</span>)</span><br><span class="line">p.then(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="number">8</span>)</span><br><span class="line">    <span class="keyword">throw</span> (<span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">&#x27;错误2&#x27;</span>))</span><br><span class="line">&#125;).catch(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;我错了&quot;</span>)</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// 1, 2, 5, 7, 3, 4, 8, 报错(没有相邻的catch处理&#x27;错误2&#x27;), 6</span></span><br><span class="line"><span class="comment">// 1, 2, 5, 7, 3, 4, 8, &quot;我错了&quot;, 6</span></span><br></pre></td></tr></table></figure>

<p>至此，程序执行完成，输出顺序为：<strong>1,2,5,7,3,4,8，”我错了”，6</strong></p>
<p><strong>再看这个例子</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;1&quot;</span>);</span><br><span class="line"><span class="built_in">setTimeout</span>(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;2&quot;</span>);</span><br><span class="line">    <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="params">resolve</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&quot;3&quot;</span>);</span><br><span class="line">        resolve();</span><br><span class="line">    &#125;).then(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&quot;4&quot;</span>);</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="params">resolve</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;5&quot;</span>);</span><br><span class="line">    resolve();</span><br><span class="line">&#125;).then(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;6&quot;</span>);</span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">setTimeout</span>(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;7&quot;</span>);</span><br><span class="line">    <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="params">resolve</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&quot;8&quot;</span>);</span><br><span class="line">        resolve();</span><br><span class="line">    &#125;).then(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&quot;9&quot;</span>);</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>输出顺序应该为？</p>
<p>咱们先来分析一波</p>
<ul>
<li><p>首先同步队列最优先，而且由上往下一行一行执行</p>
<p>首先输出 <strong>1</strong></p>
</li>
<li><p>第一个setTimeout入<code>宏队列1</code></p>
</li>
<li><p>new Promise()声明新的promise，当作同步任务执行，把resolve入微队列，把.then()放入当前<code>宏任务0</code>的微队列</p>
</li>
<li><p>输出 <strong>5 6</strong></p>
</li>
<li><p>往下执行，最后一个setTimeout加入<code>宏队列2</code></p>
</li>
<li><p>执行<code>宏队列1</code>的任务，在<code>宏队列1</code>中，console.log(1)是同步任务，优先输出</p>
</li>
<li><p>输出 <strong>2</strong></p>
</li>
<li><p>执行<code>宏队列1</code>中的微队列任务console.log(3)和console.log(4)</p>
</li>
<li><p>输出 <strong>3 4</strong></p>
</li>
<li><p>执行<code>宏队列2</code>的任务，在<code>宏队列2</code>中，console.log(7)是同步任务，优先输出</p>
</li>
<li><p>输出 <strong>7</strong></p>
</li>
<li><p>执行<code>宏队列2</code>中的微队列任务console.log(8)和console.log(9)</p>
</li>
<li><p>输出 <strong>8 9</strong> </p>
</li>
</ul>
<p>总结，最终输出 <strong>1 5 6 2 3 4 7 8 9</strong></p>
<img src="/blogs/2022/07/10/JS%E4%BA%8B%E4%BB%B6%E5%BE%AA%E7%8E%AF%E6%9C%BA%E5%88%B6/1.png" class title="事件循环机制结果">

<p>改造一下上面代码，在微任务里面添加一个宏任务</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;1&quot;</span>);</span><br><span class="line"><span class="built_in">setTimeout</span>(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;2&quot;</span>);</span><br><span class="line">    <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="params">resolve</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&quot;3&quot;</span>);</span><br><span class="line">        resolve();</span><br><span class="line">    &#125;).then(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&quot;4&quot;</span>);</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="params">resolve</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;5&quot;</span>);</span><br><span class="line">  	<span class="built_in">setTimeout</span>(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">      <span class="built_in">console</span>.log(<span class="string">&quot;hahaha&quot;</span>);</span><br><span class="line">    &#125;)</span><br><span class="line">    resolve();</span><br><span class="line">&#125;).then(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;6&quot;</span>);</span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">setTimeout</span>(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;7&quot;</span>);</span><br><span class="line">    <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="params">resolve</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&quot;8&quot;</span>);</span><br><span class="line">        resolve();</span><br><span class="line">    &#125;).then(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&quot;9&quot;</span>);</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>输出顺序应该是：</p>
<p>最终输出 <strong>1 5 6 2 3 4  hahaha  7 8 9</strong></p>
<p>过程就是</p>
<ul>
<li><p>首先同步队列最优先，而且由上往下一行一行执行</p>
<p>首先输出 <strong>1</strong></p>
</li>
<li><p>第一个setTimeout入<code>宏队列1</code></p>
</li>
<li><p>new Promise()声明新的promise，当作同步任务执行，把resolve入微队列，把 setTimeout() 入宏队列，把.then()放入当前<code>宏任务0</code>的微队列</p>
</li>
<li><p>输出 <strong>5 6</strong></p>
</li>
<li><p>往下执行，最后一个setTimeout加入<code>宏队列2</code></p>
</li>
<li><p>执行<code>宏队列1</code>的任务，在<code>宏队列1</code>中，console.log(1)是同步任务，优先输出</p>
</li>
<li><p>输出 <strong>2</strong></p>
</li>
<li><p>执行<code>宏队列1</code>中的微队列任务console.log(3)和console.log(4)</p>
</li>
<li><p>输出 <strong>3 4</strong></p>
</li>
<li><p>运行宏队列中的任务setTimeout() ,输出<strong>hahaha</strong></p>
</li>
<li><p>执行<code>宏队列2</code>的任务，在<code>宏队列2</code>中，console.log(7)是同步任务，优先输出</p>
</li>
<li><p>输出 <strong>7</strong></p>
</li>
<li><p>执行<code>宏队列2</code>中的微队列任务console.log(8)和console.log(9)</p>
</li>
<li><p>输出 <strong>8 9</strong> </p>
</li>
</ul>
<p><strong>来看下一个例子</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//&lt;script&gt;</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&#x27;start&#x27;</span>);</span><br><span class="line"> </span><br><span class="line"><span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;timeout&#x27;</span>);</span><br><span class="line">&#125;, <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"><span class="built_in">Promise</span>.resolve().then(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;promise&#x27;</span>);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&#x27;end&#x27;</span>);</span><br><span class="line"><span class="comment">//&lt;/script&gt;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>start end promise timeout</p>
<p>分析：</p>
<p>1、主线程开始执行，<code>script</code> 标签中的代码被加入到执行栈中执行，同步代码优先执行，依次输出 <code>start =&gt; end</code><br> 2、执行过程中，碰到任务源 <code>setTimeout</code> ，将 <code>setTimeout</code> 的回调函数加入到宏任务队列中<br> 3、继续执行， 碰到任务源 <code>promise</code>， 将 <code>promise</code> 的回调函数加入到微任务队列中<br> 4、end 输出后 执行栈空闲，检查微务队列，发现有一个微任务，取出进入到执行栈中执行，输出 <code>promise</code><br> 5、微任务队列中所有微任务执行完成，开始下一个循环<br> 6、检查宏任务队列中是否有可执行的宏任务，发现有一个，取出放入执行栈中执行，输出 <code>timeout</code><br> 7、执行完毕</p>
<p>参考：<a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_43842373/article/details/124485407">https://blog.csdn.net/weixin_43842373/article/details/124485407</a></p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="http://chaoyxy.cn/data/User/admin/home/desktop/images/alipay.jpg" alt=" 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="http://chaoyxy.cn/data/User/admin/home/desktop/images/wechatpay.jpg" alt=" 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blogs/tags/JavaScript/" rel="tag"># JavaScript</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blogs/2022/07/09/js%E5%A4%84%E7%90%8601%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%98/" rel="next" title="js处理01背包问题">
                <i class="fa fa-chevron-left"></i> js处理01背包问题
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blogs/2022/07/11/JS%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97/" rel="prev" title="JS实现队列">
                JS实现队列 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a target="_blank" rel="noopener" href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blogs/images/cat.jpg"
                alt="" />
            
              <p class="site-author-name" itemprop="name"></p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blogs/archives">
              
                  <span class="site-state-item-count">43</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blogs/tags/index.html">
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://gitee.com/louchaoyy" target="_blank" title="Gitee">
                      
                        <i class="fa fa-fw fa-gitee"></i>Gitee</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="louchao@louchaoyy.cn" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E4%BA%8B%E4%BB%B6%E6%98%AF%E6%80%8E%E4%B9%88%E6%89%A7%E8%A1%8C%E7%9A%84%EF%BC%9F"><span class="nav-number">1.</span> <span class="nav-text">1.事件是怎么执行的？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%90%8C%E6%AD%A5%E5%BC%82%E6%AD%A5%E4%BB%A3%E7%A0%81%E6%89%A7%E8%A1%8C%E6%B5%81%E7%A8%8B"><span class="nav-number">2.</span> <span class="nav-text">2.同步异步代码执行流程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%AE%8F%E4%BB%BB%E5%8A%A1%E5%92%8C%E5%BE%AE%E4%BB%BB%E5%8A%A1"><span class="nav-number">3.</span> <span class="nav-text">3.宏任务和微任务</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E5%AE%8F%E4%BB%BB%E5%8A%A1%EF%BC%88macrotask%EF%BC%89"><span class="nav-number">3.1.</span> <span class="nav-text">1.宏任务（macrotask）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E5%BE%AE%E4%BB%BB%E5%8A%A1%EF%BC%88microtask%EF%BC%89"><span class="nav-number">3.2.</span> <span class="nav-text">2.微任务（microtask）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E4%BA%8B%E4%BB%B6%E5%BE%AA%E7%8E%AF%E6%B5%81%E7%A8%8B%E6%80%BB%E7%BB%93"><span class="nav-number">3.3.</span> <span class="nav-text">3.事件循环流程总结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E5%90%8C%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1"><span class="nav-number">4.</span> <span class="nav-text">4.同异步任务</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Chaoyxy</span>

  
</div>

<span id="busuanzi_container_site_pv">
    本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<!--

  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.3</div>
-->




        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/blogs/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/blogs/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/blogs/js/src/motion.js?v=5.1.3"></script>



  
  


  <script type="text/javascript" src="/blogs/js/src/affix.js?v=5.1.3"></script>

  <script type="text/javascript" src="/blogs/js/src/schemes/pisces.js?v=5.1.3"></script>



  
  <script type="text/javascript" src="/blogs/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/blogs/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/blogs/js/src/bootstrap.js?v=5.1.3"></script>



  


  

    
      <script id="dsq-count-scr" src="https://Chaoyxy.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'https://louchaoyy.gitee.io/blogs/2022/07/10/JS%E4%BA%8B%E4%BB%B6%E5%BE%AA%E7%8E%AF%E6%9C%BA%E5%88%B6/';
          this.page.identifier = '2022/07/10/JS事件循环机制/';
          this.page.title = 'JS事件循环机制';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://Chaoyxy.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  




	





  














  





  

  

  

  
  

  

  

  

</body>
</html>
